#include "../../ctlProgress.h"

namespace swui
{
	bool ctlProgress::create(ctlRoot* parent, ctlRect rect, 
		ctlScrollDirection dir, const ctlStyleProgress& style)
	{
		if(parent == 0 || isCreated() || !parent->isCreated()) return false;
		std::lock_guard<decltype(_eventTrans)> gurad(
			_eventTrans.getControlEventTrans(parent));
		_control_virtual_class_end = this;
		_basic_create_(rect, parent);
		_controlType = ctlControlType::ctlProgress;
		_progress_scrolled_logic_length = 0;
		_progress_logic_length = 100;
		_progress_direction = dir;
		setStyle(style);
		return true;
	}

	ctlScrollDirection ctlProgress::getBarDirection()
	{
		return _progress_direction;
	}
	bool ctlProgress::setMaxLogicLength(int length)
	{
		_progress_logic_length = length;
		_progress_scrolled_logic_length = 
			static_cast<int>(length * getPercentProgress());
		return true;
	}
	int ctlProgress::getMaxLogicLength()
	{
		return _progress_logic_length;
	}
	bool ctlProgress::setPercentProgress(float percent)
	{
		return setLogicProgress(
			static_cast<int>(percent * getMaxLogicLength()));
	}
	float ctlProgress::getPercentProgress()
	{
		return static_cast<float>(getLogicProgress())
			/ static_cast<float>(getMaxLogicLength());
	}
	bool ctlProgress::setLogicProgress(int logicProgress)
	{
		if(logicProgress < 0) logicProgress = 0;
		else if(logicProgress > getMaxLogicLength())
			logicProgress = getMaxLogicLength();
		ctlNotifyEvent ne;
		ne.eventType = ctlNotifyEventType::scroll;
		ctlNotifyScroll nse;
		nse.logicLength = logicProgress;
		nse.percent = static_cast<float>(logicProgress)
			/ static_cast<float>(getMaxLogicLength());
		bool allow = true;
		nse.isAllow = &allow;
		ne.info = &nse;

		_notify_event_(&ne);
		if(allow)
		{
			_progress_scrolled_logic_length = logicProgress;
			redraw();
			return true;
		}
		return false;
	}
	int ctlProgress::getLogicProgress()
	{
		return _progress_scrolled_logic_length;
	}
}